package cn.wolfcode.web.controller;

import cn.wolfcode.web.annotation.RequiredPermission;
import cn.wolfcode.web.domain.Department;
import cn.wolfcode.web.domain.Employee;
import cn.wolfcode.web.domain.Role;
import cn.wolfcode.web.page.PageResult;
import cn.wolfcode.web.query.EmployeeQueryObject;
import cn.wolfcode.web.service.IDepartmentService;
import cn.wolfcode.web.service.IEmployeeService;
import cn.wolfcode.web.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageResult pageResult = employeeService.query(qo);
       List<Department> departments = departmentService.listAll();//为了让用户有多个部门对象可以选择,查询出部门集合
        model.addAttribute("pageResult",pageResult);
       model.addAttribute("departments", departments);//把多个部门集合共享到jsp,让用户可以挑选


        return "employee/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model) {
        System.out.println("11");
        if (id!=null) {
            Employee employee = employeeService.get(id);
            List<Role> roles = roleService.selectById(employee.getId());
            model.addAttribute("roles",roles);
            List<Department> departments = departmentService.listAll();
            model.addAttribute("departments",departments);
            model.addAttribute("employee",employee);


        }
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);
        return "employee/input";
    }
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(value = "xx删除",expression = "xxx:delete")
    public String saveOrUpdate(Employee employee,Long[] ids) {
        System.out.println("1113222");
        System.out.println("11");
        if(employee.getId()!=null){
            employeeService.update(employee);
        }else {
            employeeService.save(employee);
        }
        if (ids!=null&&ids.length>0) {
            for (Long roleId : ids) {
                employeeService.insertRelation(employee.getId(), roleId);
            }
        }
        return "redirect:/employee/list.do";//employee/saveOrUpdate.do    "/department/saveOrUpdate.do"
    }
}
